package pub.cleangao.memm.registry;


import lombok.Setter;
import pub.cleangao.memm.util.UnionKey;

@Setter
public class RpcServiceConfig {
    /**
     * service version
     */
    private String version;
    /**
     * when the interface has multiple implementation classes, distinguish by group
     */
    private String group;

    /**
     * target service
     */
    private Object serviceImpl;

    /**
     * 降级类
     */
    private Class<?> fallback;

    /**
     * 超时
     */
    private long timeout;

    public String getRpcServiceName() {
        String serviceName = getServiceName();
        if (serviceName == null) {
            return null;
        }
        return UnionKey.getRpcServiceName(serviceName,getGroup(),getVersion());
    }

    private String getServiceName() {
        if (serviceImpl.getClass().getInterfaces().length == 0) {
            return null;
        }
        return this.serviceImpl.getClass().getInterfaces()[0].getCanonicalName();
    }

    public String getVersion() {
        if (version == null || version.isBlank()) {
            return "v";
        }
        return version;
    }

    public String getGroup() {
        if (group == null || group.isBlank()) {
            return "g";
        }
        return group;
    }

    public Object getServiceImpl() {
        return serviceImpl;
    }

    public Class<?> getFallback() {
        return fallback;
    }

    public long getTimeout() {
        return timeout;
    }
}
